﻿using RTW.DAL;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RTW.OA.App_Code
{
    /// <summary>
    /// Session帮助工具
    /// </summary>
    public class SessionUtil
    {
        private static IRepository<user> ERPUserRepository;
        private static IRepository<role> ERPJiaoSeRepository;
        private static IRepository<user_online> ERPOnLineRepository;
        public SessionUtil(IRepository<user> erpuserrepository, IRepository<role> erpjiaoserepository, IRepository<user_online> erponlinerepository)
        {
            ERPUserRepository = erpuserrepository;
            ERPJiaoSeRepository = erpjiaoserepository;
            ERPOnLineRepository = erponlinerepository;
        }

        //判断Session是否有效
        public static void CheckSession()
        {
            //测试的时候注释掉
            try
            {
                if (System.Web.HttpContext.Current.Session["UserInfo"] == null)
                {
                    //检查Cookies
                    string adminname = CookieUtil.GetCookie("AdminName", "DTcms"); //解密用户名
                    string adminpwd = CookieUtil.GetCookie("AdminPwd", "DTcms");
                    if (adminname != "" && adminpwd != "")
                    {
                        string SqlSTr = "select * from ERPUser where UserName='" + adminname + "'";
                        user MyDataRow = ERPUserRepository.SqlWithParamsSingle(SqlSTr);

                        if (MyDataRow == null)
                        {
                            System.Web.HttpContext.Current.Response.Write("<script>alert('登录信息安全时限过期，请重新登录！');top.location='../login.aspx'</script>");
                        }
                        else
                        {
                            if (adminpwd == MyDataRow.USER_PWD)
                            {
                                if (ERPOnLineRepository.Any(MyDataRow.USER_ID))
                                {
                                    System.Web.HttpContext.Current.Session["UserID"] = MyDataRow.USER_ID;
                                    System.Web.HttpContext.Current.Session["UserName"] = MyDataRow.USER_NAME;
                                    System.Web.HttpContext.Current.Session["Password"] = MyDataRow.USER_PWD;
                                    System.Web.HttpContext.Current.Session["JiaoSe"] = MyDataRow.USER_PRIV;
                                    System.Web.HttpContext.Current.Session["Department"] = MyDataRow.DEPT_ID;
                                    System.Web.HttpContext.Current.Session["TrueName"] = MyDataRow.TRUE_NAME;
                                    System.Web.HttpContext.Current.Session["ZhiWei"] = MyDataRow.POST_DEPT;
                                    System.Web.HttpContext.Current.Session["QuanXian"] = ERPJiaoSeRepository.SqlWithParamsDynamic("select QuanXian from ERPJiaoSe where JiaoSeName in(" + "'" + MyDataRow.POST_PRIV.Replace(",", "','") + "'" + ")");
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<script>alert('登录信息安全时限过期，请重新登录！');top.location='../login.aspx'</script>");
            }
        }
        //获得Session中的值
        public static string GetSessionValue(string SessionKey)
        {
            //测试时候使用，不掉线
            try
            {
                CheckSession();
                return System.Web.HttpContext.Current.Session[SessionKey].ToString();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<script>alert('登录信息安全时限过期，请重新登录！');top.location='../login.aspx'</script>");
                return "NoLogin";
            }
        }
        //设置Session中的值
        public static void SetSessionValue(string SessionKey, string ValueStr)
        {
            System.Web.HttpContext.Current.Session[SessionKey] = ValueStr;
        }
    }
}
